{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.反转字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nohtyp'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "st=\"python\"\n",
    "# 方法1\n",
    "''.join(reversed(st))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nohtyp'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法2\n",
    "st[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.字符串切片操作\n",
    "---查找替换3或5的倍数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1',\n",
       " '2',\n",
       " 'java',\n",
       " '4',\n",
       " 'python',\n",
       " 'java',\n",
       " '7',\n",
       " '8',\n",
       " 'java',\n",
       " 'python',\n",
       " '11',\n",
       " 'java',\n",
       " '13',\n",
       " '14']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[str(\"java\"[i%3*4:]+\"python\"[i%5*6:] or i) for i in range(1,15)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.join 串联字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "mystr=['1',\n",
    " '2',\n",
    " 'java',\n",
    " '4',\n",
    " 'python',\n",
    " 'java',\n",
    " '7',\n",
    " '8',\n",
    " 'java',\n",
    " 'python',\n",
    " '11',\n",
    " 'java',\n",
    " '13',\n",
    " '14']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1,2,java,4,python,java,7,8,java,python,11,java,13,14'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "','.join(mystr) #用逗号作为连接符连接字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  4.字符串的字节长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def str_byte_len(mystr):\n",
    "    return (len(mystr.encode('utf-8')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_byte_len('i love python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_byte_len('中文长度') #中文3个字节/字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 以下是正则部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.查找第一个匹配串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, 13)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s='i love python very much'\n",
    "pat='python'\n",
    "r=re.search(pat,s) #在s中寻找pat所描述的字符串\n",
    "r.span()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.查找所有1的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(9, 10), match='1'>\n",
      "<re.Match object; span=(15, 16), match='1'>\n"
     ]
    }
   ],
   "source": [
    "s='江苏省无锡市人民路1号2楼201'\n",
    "pat='1'\n",
    "r=re.finditer(pat,s)\n",
    "for i in r:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.\\d匹配数字[0-9]\n",
    "findall找出全部位置的所有匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['20', '13', '59']\n"
     ]
    }
   ],
   "source": [
    "s='一共20行代码运行时间13.59s'\n",
    "pat=r'\\d+' # + 表示匹配数字(\\d表示数字的通用字符)1次或多次\n",
    "r=re.findall(pat,s)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.匹配浮点数和整数\n",
    "?表示前一个字符匹配0或1次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "s='一共20行代码运行时间13.59s'\n",
    "pat=r'd+\\.?\\d+' #?表示匹配小数点(\\.)0次或1次,bug:不能匹配到个位数的整数\n",
    "r=re.findall(pat,s)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['20', '13.59']\n"
     ]
    }
   ],
   "source": [
    "pat=r'\\d+\\.\\d+|\\d+' #A/B,匹配A,失败再匹配B\n",
    "r=re.findall(pat,s)\n",
    "print(r)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
